<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Auth;

class LoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest', [
            'only' => ['create']
        ]);
    }

    /**
     *  显示登陆界面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('admin.login.create');
    }

    public function store(Request $request)
    {
        // 判请求断参数是否符合规则
        $credentials = $this->validate($request, [
            'email' => 'required|email|max:50',
            'password' => 'required'
        ]);
        // 验证账号密码,同时是否长时间保持登陆状态
        if (Auth::attempt($credentials, $request->has('remember'))) {
            // 获取用户activated字段,判断是否激活
            //if (Auth::user()->activated) {
                return redirect()->route('home', [Auth::user()]);
            //} else {
            //    Auth::Logout();
            //    session()->flash('warning', '你的账号未激活，请检查邮箱中的注册邮件进行激活。');
            //    return redirect('/login');
            //}
        } else {
            session()->flash('danger', '账号或密码错误');
            return redirect()->back();
        }
    }

    public function destroy()
    {
        Auth::logout();
        session()->flash('success', '退出成功');
        return redirect('/login');
    }


    public function forgetcreate()
    {
        return view('admin.login.forget');
    }

    public function forgetstore(Request $request)
    {
        // 判请求断参数是否符合规则
        $credentials = $this->validate($request, [
            'email' => 'required|email|max:50',
        ]);

        $user = User::where('email',$request->email)->first();
        if($user){

        }else{
            session()->flash('danger', '邮箱不存在,请输入正确的邮箱');
            return redirect()->back();
        }
    }
}
